<script setup>

import {getAll} from '@/api/system/enterprise/enterpriseXx'
import {listTree} from "@/api/system/park/building";
import {
  addEmployee,
  getEmployeeAll,
  getEmployeeById,
  updateEmployee,
  delEmployee,
  shengqing
} from "@/api/system/apartment/staff";
import th from "element-ui/src/locale/lang/th";

export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 3
      },
      total: 1,
      tableData: [],
      enterpriseListAll: [],
      bulidingTree: [],
      floorList: [],
      roomList: [],
      dialogVisible: false,
      ruleForm: {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""

      }, rules: {
        enterpriseName: [
          {required: true, message: '请输入企业名称姓名', trigger: 'blur'},
        ],
        enterprisePrincipal: [
          {required: true, message: '请输入企业负责人', trigger: 'blur'},
        ]
      },
    }
  }, created() {
    let that = this;
    getAll().then(function (data) {
      that.enterpriseListAll = data.data
    }),
      listTree().then(function (data) {
        that.bulidingTree = data.data;
      })
    that.queryMethods();

  }, methods: {
    queryMethods() {
      let that = this;
      getEmployeeAll(that.queryParams).then(function (data) {
        console.log(data)
        that.tableData = data.data.list;
        that.queryParams.pageNum = data.data.pageNum;
        that.queryParams.pagSize = data.data.pageSize;
        that.total = data.data.total;
      })
    },
    //树改变
    changBuliding() {
      let that = this;

      that.floorList = null;
      that.roomList = null;
      that.ruleForm.roomId = "";
      that.ruleForm.floorId = "";

      let chooseBulidingId = that.ruleForm.bulidingId;
      for (let i = 0; i < that.bulidingTree.length; i++) {
        if (that.bulidingTree[i].id == chooseBulidingId) {
          that.floorList = that.bulidingTree[i].children;
        }
      }
    },
    changFloor() {
      let that = this;

      that.ruleForm.roomId = "";
      that.roomList = null;

      let chooseFloorId = that.ruleForm.floorId;
      console.log("chooseFloorId>>>>>>>>>>>> " + chooseFloorId)
      for (let i = 0; i < that.floorList.length; i++) {
        if (that.floorList[i].id == chooseFloorId) {
          that.roomList = that.floorList[i].children;
        }
      }

    },
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryParams");
      this.queryMethods();
    },
    addStaffShow() {
      this.dialogVisible = true;
      this.ruleForm = {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""
      }

    },
    submitForm(formName) {
      let that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.ruleForm.id == "" || that.ruleForm.id == undefined) {
            addEmployee(that.ruleForm).then((res) => {
              if (res.code == 200) {
                that.$message.success("添加成功");
              } else {
                that.$message.error("添加失败")
              }
              that.dialogVisible = false;
              that.queryMethods();
            })
          } else {
            updateEmployee(that.ruleForm).then((res) => {
              if (res.code == 200) {
                that.$message.success("修改成功");
              } else {
                that.$message.error("修改失败")
              }
              that.dialogVisible = false;
              that.queryMethods();
            })
          }
        } else {
          return false;
        }
      });
    },
    updateShow(id) {
      let that = this;
      getEmployeeById(id).then(function (data) {
        console.log(data)
        that.ruleForm.id = data.data.id;
        that.ruleForm.checkInTime = data.data.checkInTime;
        that.ruleForm.enterpriseId = data.data.enterpriseId;
        that.ruleForm.employeeName = data.data.employeeName;
        that.ruleForm.employeePhone = data.data.employeePhone;
        that.ruleForm.tenancyTerm = data.data.tenancyTerm;
        that.ruleForm.rent = data.data.rent;
        that.ruleForm.bulidingId = data.data.bulidingId;
        that.changBuliding();
        that.ruleForm.floorId = data.data.floorId;
        that.changFloor();
        that.ruleForm.roomId = data.data.roomId;
      })
      that.dialogVisible = true;
    },
    del(id) {
      let that = this;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delEmployee(id).then(function (data) {
          if (data.code == 200) {
            that.$message.success("删除成功");
          } else {
            that.$message.error("删除失败")
          }
          that.queryMethods();
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    shenqing(id) {
      let that = this;
      shengqing(id).then(function (data) {
        if (data.code == 200) {
          that.$message.success("申请成功");
        } else {
          that.$message.error("申请失败")
        }
        that.queryMethods();
      })

    },
    handleSizeChange(pageSize) {
      this.queryParams.pageSize = pageSize;
      this.queryMethods();
    },
    handleCurrentChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.queryMethods();
    },
  }

}
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="租客名字">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入租客名字"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="租客电话">
        <el-input
          v-model="queryParams.employeePhone"
          placeholder="请输入租客电话"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="企业名称">
        <el-select
          v-model="queryParams.enterpriseId"
          placeholder="请选择企业"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="item in enterpriseListAll"
            :key="item.enterpriseName"
            :label="item.enterpriseName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryMethods()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" @click="addStaffShow">新增</el-button>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="80">
      </el-table-column>
      <el-table-column
        prop="employeeName"
        label="住户姓名"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        prop="employeePhone"
        label="住户电话"
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="enterpriseName"
        label="单位"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="objName"
        label="房间号"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="checkInTime"
        label="申请入住时间"
        align="center"
        width="140">
      </el-table-column>
      <el-table-column
        prop="leaseDeadline"
        label="租约期限"
        align="center"
        width="140">
      </el-table-column>
      <el-table-column
        label="租约状态"
        align="center"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.leaseStatus==1">
            <el-tag type="warning">未审核</el-tag>
          </span>
          <span v-if="scope.row.leaseStatus==2">
            <el-tag type="success">正常</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="rent"
        label="月租金（元）"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        label="租期（月）"
        align="center"
        width="110">
        <template slot-scope="scope">
          {{ scope.row.tenancyTerm }}月
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230">
        <template slot-scope="scope">
          <el-tooltip content="公寓申请">
            <el-button type="success" v-if="scope.row.leaseStatus==1" icon="el-icon-s-grid"
                       @click="shenqing(scope.row.id)"
                       circle></el-button>
          </el-tooltip>
          <el-tooltip content="编辑">
            <el-button type="primary" icon="el-icon-edit" @click="updateShow(scope.row.id)" circle></el-button>
          </el-tooltip>
          <el-tooltip content="删除">
            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryParams.pageNum"
        :page-size="this.queryParams.pageSize"
        :page-sizes="[1,2,3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="添加入住员工信息"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="租客姓名" prop="employeeName">
          <el-input v-model="ruleForm.employeeName"></el-input>
        </el-form-item>
        <el-form-item label="租客电话" prop="employeePhone">
          <el-input v-model="ruleForm.employeePhone"></el-input>
        </el-form-item>
        <el-form-item label="企业">
          <el-select
            v-model="ruleForm.enterpriseId"
            placeholder="请选择企业"
            clearable
            size="small"
            style="width: 200px"
          >
            <el-option
              v-for="item in enterpriseListAll"
              :key="item.enterpriseName"
              :label="item.enterpriseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="居住位置">
          <el-select
            @change="changBuliding"
            v-model="ruleForm.bulidingId"
            placeholder="楼栋类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in bulidingTree"
                       :key="o.buildingName"
                       :label="o.buildingName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            @change="changFloor"
            v-model="ruleForm.floorId"
            placeholder="楼层"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in floorList"
                       :key="o.floorName"
                       :label="o.floorName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            v-model="ruleForm.roomId"
            placeholder="房间"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in roomList"
                       :key="o.roomName"
                       :label="o.roomName"
                       :value="o.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租期（月）">
          <el-input v-model="ruleForm.tenancyTerm"></el-input>
        </el-form-item>
        <el-form-item label="租金（元）">
          <el-input v-model="ruleForm.rent"></el-input>
        </el-form-item>
        <el-form-item label="入住时间">
          <el-date-picker
            v-model="ruleForm.checkInTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">

</style>
